package Java.utils;//package Java.utils;

import Java.dao.SQL_Readin;
import Java.pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/*
登录注册界面
 */
public class Login_In extends JFrame implements ActionListener{
    private boolean  isTeacher=false;
    public  PreparedStatement for_password_check;
    private String auto_login ="select * from UserInformation WHERE Password=? and ID =? and Identity=?";
    private String sign_up_sql = "insert into UserInformation (ID,Password,Identity)values(?,?,?)";
    private JTextField numField;private  JPasswordField passwordField;
    private JTextField nameField;
    private JRadioButton nanButton; private JRadioButton nvButton;
    private ButtonGroup buttonGroup1=new ButtonGroup();
    private JRadioButton teacherButton;private JRadioButton studentButton;
    private  ButtonGroup buttonGroup = new ButtonGroup();
    private JButton loginButton;private  JButton registButton;
    private JLabel label1;private  JLabel label2;private JLabel label3;private JLabel label4;
    private Box boxV;private Box boxH1;private Box boxH2;private Box boxH3;private  Box boxH4;private Box boxH5;private Box boxH6;
    public Login_In() {
        setBounds(10, 10, 400, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());setVisible(true);setTitle("登录界面");
        label1=new JLabel("账号");label2=new JLabel("密码");
        label3=new JLabel("姓名");label4=new JLabel("性别");
        numField=new JTextField(20);passwordField=new JPasswordField(20);
        nameField=new JTextField(20);
        teacherButton=new JRadioButton("教师");studentButton=new JRadioButton("学生");
        nanButton=new JRadioButton("男");nvButton=new JRadioButton("女");
        loginButton=new JButton("登录");registButton=new JButton("注册");
        boxV=Box.createVerticalBox();boxH1=Box.createHorizontalBox();
        boxH2=Box.createHorizontalBox();boxH3=Box.createHorizontalBox();
        boxH4=Box.createHorizontalBox();
        boxH5=Box.createHorizontalBox();boxH6=Box.createHorizontalBox();
        boxH1.add(label1);boxH1.add(numField);
        boxH5.add(label3);boxH5.add(nameField);
        boxH6.add(label4);boxH6.add(nanButton);boxH6.add(nvButton);
        boxH2.add(label2);boxH2.add(passwordField);
        buttonGroup.add(studentButton);buttonGroup.add(teacherButton);
        buttonGroup1.add(nanButton);buttonGroup1.add(nvButton);
        boxH3.add(studentButton);boxH3.add(teacherButton);
        boxH4.add(loginButton);boxH4.add(registButton);
        boxV.add(boxH1);/*boxV.add(boxH5);*/boxV.add(boxH2);/*boxV.add(boxH6);*/boxV.add(boxH3);boxV.add(boxH4);
        add(boxV);
        setVisible(true);
        loginButton.addActionListener(this);registButton.addActionListener(this);
        teacherButton.addActionListener(this);studentButton.addActionListener(this);
        //nanButton.addActionListener(this);nvButton.add(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==loginButton)//点击登陆
        {
            boolean judge=false;
            try {//设置预处理语句
                for_password_check= SQL_Readin.con.prepareStatement(auto_login);//预处理语句变为寻找
                for_password_check.setString(1,passwordField.getText());
                for_password_check.setString(2,numField.getText());
                if(isTeacher) for_password_check.setInt(3,1);
                else for_password_check.setInt(3,0);
                SQL_Readin.activatesql(for_password_check);//执行语句，对resultset 赋值
                if(SQL_Readin.rs.next()) {
                    judge=true;
                    User.num=numField.getText();
                }
            } catch (SQLException ex) {
                System.out.println(ex);
                throw new RuntimeException(ex);
            }
            if(judge){//登陆成功
                System.out.println("success");
            }
            else //密码或账户不匹配，弹出提示消息
            {
                JOptionPane.showMessageDialog(this,"密码错误","提示",JOptionPane.ERROR_MESSAGE);
                passwordField.setText("");//清空密码
            }
            try {
                for_password_check.close();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        else if(e.getSource()==registButton) {
            try {
                for_password_check = SQL_Readin.con.prepareStatement(sign_up_sql);//预处理语句变为插入
                for_password_check.setString(1,numField.getText());
                for_password_check.setString(2,passwordField.getText());
                if(isTeacher) for_password_check.setInt(3,1);
                else for_password_check.setInt(3,0);
                //System.out.println(for_password_check);
                int executeUpdate = for_password_check.executeUpdate();
                if(executeUpdate<=0){
                    JOptionPane.showMessageDialog(this,"注册失败","提示",JOptionPane.ERROR_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(this,"注册成功","提示",JOptionPane.PLAIN_MESSAGE);
                }
                // SQL_Readin.activatesql(for_password_check);//执行语句，对resultset 赋值
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            try {
                for_password_check.close();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        else if(e.getSource()==teacherButton)  isTeacher=true;//老师为真
        else if(e.getSource()==studentButton)   isTeacher=false;//学生为假
    }
}
